package com.sm.honkon.ui.fragment

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import com.blankj.utilcode.util.ToastUtils
import com.sm.honkon.R
import com.sm.honkon.app.base.BaseFragment
import com.sm.honkon.app.event.ReadDataEvent
import com.sm.honkon.app.ext.getTimeFormat
import com.sm.honkon.app.utils.CommondData
import com.sm.honkon.app.utils.SerialPortUtil
import com.sm.honkon.databinding.FrgMainBinding
import com.sm.honkon.ui.model.MainViewModel
import kotlinx.android.synthetic.main.frg_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainFragment : BaseFragment<MainViewModel, FrgMainBinding>() {
    override fun layoutId(): Int = R.layout.frg_main

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        mDatabind.click = Click()
    }

    override fun createObserver() {
        super.createObserver()
        appViewModel.dateLiveData.observe(this) {
            val currentTimeMillis = System.currentTimeMillis()
            tv_time.text = currentTimeMillis.getTimeFormat("hh:mm")
            tv_date.text =
                currentTimeMillis.getTimeFormat("MM/dd") + "\n" + currentTimeMillis.getTimeFormat("EEE")
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onDataRead(event:ReadDataEvent){
        event.data.let {
            if (isVisible.not()){
                return
            }
            if (it[0].and(0xFF) != 0x01)
                return
            val type = it[1].and(0xFF)
            val result = it[3].and(0xFF)
            ToastUtils.showShort("收到数据")
            when (type) {
                0x11 -> {
                    launch?.cancel()
                    if (result == 0x00) {
                        ToastUtils.showShort("联机成功")
                        nav().navigateAction(R.id.action_mainFragment_to_laserTherapyFragment)
                    } else {
                        ToastUtils.showShort("已断开")
                    }
                    laser_therapy.isEnabled = true
                }

            }

        }
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this)
        super.onDestroyView()
    }

    var launch: Job? = null

    inner class Click {
        fun laserTherapy() {
//            nav().navigateAction(R.id.action_mainFragment_to_laserTherapyFragment)
            laser_therapy.isEnabled = false
            ToastUtils.showShort("正在连接中,请耐心守候")
            SerialPortUtil.sendSerialPort(CommondData.START_CONTROL)
            launch = lifecycleScope.launch(Dispatchers.Main) {
                delay(10_000)
                laser_therapy.isEnabled = true
                ToastUtils.showShort("连接超时")
            }
        }

        fun clientsManagement() {
            if (laser_therapy.isEnabled)
                nav().navigateAction(R.id.action_mainFragment_to_clientsManagementFragment)
        }

        fun signedUse() {
            if (laser_therapy.isEnabled)
                nav().navigateAction(R.id.action_mainFragment_to_signUseLoginFragment)
        }

        fun more() {
            if (laser_therapy.isEnabled)
                nav().navigateAction(R.id.action_mainFragment_to_moreFragment)
        }

        fun setting() {
            if (laser_therapy.isEnabled)
                nav().navigateAction(R.id.action_mainFragment_to_settingFragment)
        }
    }
}